package com.hhx.androidrxmvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.hhx.androidrxmvp.app.BaseApplication;
import com.hhx.androidrxmvp.base.mvp.BasePresenter;
import com.hhx.androidrxmvp.dagger.component.ActivityComponent;
import com.hhx.androidrxmvp.dagger.component.DaggerActivityComponent;
import com.hhx.androidrxmvp.dagger.module.ActivityModule;

import javax.inject.Inject;

/**
 * Created by Administrator on 2019/3/16.
 */

public abstract class BaseMvpActivity<P extends BasePresenter> extends SimpleActivity {

    protected static String TAG = "BaseMvpActivity";

    @Inject
    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initInjector();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        super.onCreate(savedInstanceState);
    }

    protected void initInjector() {

    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        super.onDestroy();
    }

    public ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .applicationComponent(BaseApplication.getInstance().getApplicationComponent())
                .activityModule(getActivityModule())
                .build();
    }

    public ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

}
